<?php
namespace app\smart\model;

class Poetry
{
    public $id;
    public $title;
    public $author;
    public $content;
    public $options;

    function __construct($data)
    {
        $this->id = $data['id'];
        $this->title = $data['title'];
        $this->author = $data['author'];
        $this->content = $data['content'];
        $this->puzzle = isset($data['puzzle']) ? $data['puzzle'] : [['split'=>1,'cols'=>5]];
    }

    function getSentence()
    {
        $sentence = mb_split('[，。？]', $this->content);
        unset($sentence[count($sentence) - 1]);
        return $sentence;
    }

    /**
     * 生成二维矩阵
     *
     * @return array
     */
    function makeRandomWords($puzzleIndex = 0)
    {
        $sentence = $this->getSentence();
        $data = [];
        $puzzle = isset($this->puzzle[$puzzleIndex]) ? $this->puzzle[$puzzleIndex] : $this->puzzle[0];
        foreach($sentence as $line){
            // 每行分配规则
            $temp = $this->splitSentenceWords($line, $puzzle['split']);
            $data = array_merge($data, $temp);
        }
        shuffle($data);

        $col = $puzzle['cols']; // 多少行
        $row = ceil(count($data) / $col);
        for($i=0; $i < $row; $i++){
            $result[] = array_slice($data, $i * $col, $col);
        }
        return $result;
    }

    function draw($puzzleIndex)
    {
        $data = $this->makeRandomWords($puzzleIndex);

        echo <<< HTML
<!DOCTYPE html>
<html>
    <head>
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimal-ui,viewport-fit=cover"/>
    <meta name="format-detection" content="telephone=no"/>
    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <meta charset="utf-8"/>
    <title>$this->title</title>
</head>
<body>

HTML;
        echo '<center>';
        echo '<h1>'.$this->title.'</h1>';
        echo '<p>'.join(' ', $this->author).'</p>';
        echo '<table border="1">';
        foreach($data as $rows){
            echo '<tr>';
            foreach ($rows as $col){
                echo '<td height="120" width="120" align="center"><font size="+3">'.$col.'</font></td>';
            }
            echo '</tr>';
        }
        echo '</table>';
        echo '</center>';
//        echo '<ul class="am-avg-sm-4 boxes">';
//        foreach($data as $item){
//            echo '<li class="box">'.$item.'</li>';
//            $index ++;
//        }
//        echo '</ul>';
    }

    /**
     * 根据指定数量，分隔句子的字串
     *
     * @param string $line
     * @param array|int $args 字符分组方式，如[2, 3, 2]分别二，三，二个字一组
     * @return array
     */
    function splitSentenceWords($line, $args)
    {
        $index = 0;
        $data = [];
        if($args == 1){
            $len = mb_strlen($line);
            $args = [];
            for($i=0; $i<$len; $i++){
                array_push($args, 1);
            }
        }

        foreach($args as $arg){
            $data[] = mb_substr($line, $index, $arg);
            $index += $arg;
        }

        return $data;
    }
}

